package com.btpn.custody.web.m003.action;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import com.btpn.custody.common.Constant;
import com.btpn.custody.dao.IBranch;
import com.btpn.custody.dao.IUser;
import com.btpn.custody.entity.Branch;
import com.btpn.custody.entity.User;
import com.btpn.custody.service.IMasterService;
import com.btpn.custody.web.m003.model.Report04Model;
import com.btpn.infra.utils.MyDateUtils;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

@ParentPackage(value="custody")
@Results({
	@Result(name="input", location="t_report04_form")
})
@Validations(
	requiredStrings = {
		@RequiredStringValidator(type=ValidatorType.SIMPLE, fieldName="fromDate", message="From date must be choosed")
    }
)
public class Jasper04Action extends ActionSupport implements Preparable {

	/*=============================================== C O N S T A N T =========================================*/
	private static final long serialVersionUID = -6764404613153478294L;
	
	/*=============================================== M E M B E R =============================================*/		
	@Autowired
	private IBranch branchDaoHibernate;
	
	@Autowired
	private IUser userDaoHibernate;
		
	@Autowired
	private IMasterService masterDataService;
	
	private List<Branch> branchs;
	private String fromDate;
	private String toDate;
	private Long branchId;
	private List<Report04Model> reportModels;
	
	/*=============================================== B E H A V I O R =========================================*/	
	public void init() {
		User user = userDaoHibernate.get(SecurityContextHolder.getContext().getAuthentication().getName());
		
		// init branch
		if (user.getArea().isJakarta()) {			
			branchs = branchDaoHibernate.findByAreaId(user.getArea().getId());
			Branch b = new Branch();b.setId(-1L);b.setName("all branch");
			branchs.add(0, b);
		} else {			
			branchs = branchDaoHibernate.findByAreaId(user.getArea().getId());
		}
	}
	
	@SkipValidation
	@Action(value = "/report04-form", results = { @Result(name="success", location="t_report04_form") })
	public String execute() throws Exception {
				
		return SUCCESS;
	}

	@Action(value = "/report04", results = {
			@Result(name="success", type="jasper", 
					params={"location", "/jasper/report04.jasper", "dataSource", "reportModels", "contentDisposition", "attachment", "format", "XLS", "documentName", "Close Account"}),
			@Result(name="none", type="redirectAction", params = {"actionName", "report04-form"} )
	})
	public String showReport04() throws Exception {

		Date from = MyDateUtils.strToDate(Constant.FORMAT_PRESENTATION, fromDate);
		Date to = MyDateUtils.strToDate(Constant.FORMAT_PRESENTATION, toDate);

		if (from == null) {
			addFieldError(fromDate, "Invalid date format");
			return NONE;
		}
		
		if (to == null) {
			to = DateUtils.addSeconds(DateUtils.addDays(from, 1), -1);
		} else {
			to = DateUtils.addSeconds(DateUtils.addDays(to, 1), -1);
		}
		
		reportModels = masterDataService.findReport04(branchId, from, to);  
		
		if (reportModels.size()==0) {
			addActionMessage("Empty data");
			return NONE;
		}
		
		return SUCCESS;
	}
	
	@Override
	public void prepare() throws Exception {

		init();
	}
	
	/*=============================================== G E T T E R   &   S E T T E R ===========================*/
	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public List<Branch> getBranchs() {
		return branchs;
	}

	public void setBranchs(List<Branch> branchs) {
		this.branchs = branchs;
	}

	public Long getBranchId() {
		return branchId;
	}

	public void setBranchId(Long branchId) {
		this.branchId = branchId;
	}

	public List<Report04Model> getReportModels() {
		return reportModels;
	}

	public void setReportModels(List<Report04Model> reportModels) {
		this.reportModels = reportModels;
	}
}